///////////// vars ///////////////////
unsigned long fuel_last_time=0;
unsigned int fuel_count; // preload anzahl von 10tel ml 
int fuel_out; // preload (liter * 10) verbraucht
int fuel_blink=500;
int fuel_max; // max
int fuel_warning=2; // bei 2 l anfangen zu blinken
///////////// vars ///////////////////
void fuel_counter(){
  if(digitalRead(19)==HIGH){
    fuel_count++;
    if(fuel_count>=(fuel_anzahl-1)){
      fuel_count=0; 
      fuel_out++;
      if(fuel_debug){
        Serial.print("fuel count: ");
        Serial.print(fuel_count);
        Serial.print(" fuel out: ");
        Serial.print(fuel_out);
        Serial.print("\n");
      };
    }; 
  };
};

void fuel_save(int *save){
  save[0]=-1; // default value
  
  if(fuel_out>fuel_max){
    save[0]=fuel_out;
  };
  save[1]=fuel_out;
  save[2]=fuel_count;
};

  
void fuel_init() {
  // pin 19 = interrupt 4
  pinMode(19,OUTPUT); // neccesary to send reset signal once
  digitalWrite(19,HIGH); // reset counter 1/2
  digitalWrite(19,LOW); //  reset counter 2/2
  pinMode(19,INPUT); // change mode back to input
  Serial.println("Fuel 12 bit counter reseted");
  attachInterrupt(4, fuel_counter, RISING ); // interrupt handler für signalwechsel 4=DigiPin 19 
  Serial.println("Fuel init done");
};

float fuel_copy(){
  float fuel_diff=(float)(fuel_max-fuel_out)/10;
  if(fuel_diff<=fuel_warning && fuel_last_time+2*fuel_blink<millis()){
    fuel_last_time=millis();
    return fuel_diff;
  } else if(fuel_diff<=fuel_warning && fuel_last_time+fuel_blink<millis()){
    return -100;
  };
  return fuel_diff;
};

void fuel_set(int counter, int out, int maximum){
  if(counter>-1){    fuel_count=counter;   };
  if(out>-1)    {    fuel_out=out;         };
  if(maximum>-1){    fuel_max=maximum;     };
  
  if(fuel_debug){
    Serial.print("=== Fuel === \n");
    Serial.print("Die neuen Speicher sind jetzt \n counter:");
    Serial.print(fuel_count);
    Serial.print("\n fuel_out:");
    Serial.print(fuel_out);
    Serial.print("\n fuel_max:");
    Serial.print(fuel_max);
    Serial.print("\n== Done == \n");
  };
};
